package hotswap.project;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jsl.NCCUtil;
import com.jsl.NCCloud21Service;
import com.jsl.action.AddProjectInterface;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.util.HashMap;
import java.util.Map;

public class AddProjectInterfaceImpl implements AddProjectInterface {
    private static final Log log = LogFactory.getLog(AddProjectInterfaceImpl.class);

    @Override
    public String execute(RequestInfo info) {
        log.info("**********************进入 AddProjectAction **********************");

        String requestid = info.getRequestid();
        String workflowid = info.getWorkflowid();
        String formtable = NCCUtil.getFormtable(workflowid);

        log.info("[AddProjectAction.execute] workflowid:" + workflowid + " formtable:" + formtable + " requestid:" + requestid);
        MainTableInfo mainTableInfo = info.getMainTableInfo();

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String mainTableInfoString = objectMapper.writeValueAsString(mainTableInfo);
            log.info("[AddProjectAction.execute] mainTableInfo:" + mainTableInfoString);
            String requestBody = JSON.toJSONString(getRequestMap(mainTableInfo));
            log.info("[AddProjectAction.execute] requestBody:" + requestBody);
            String apiUrl = "/nccloud/api/uapbd/projectmanage/project/add";

            // 拿到返回数据，传入apiUrl、请求参数
            String result = NCCloud21Service.pushData(apiUrl, requestBody);

            log.info("[AddProjectAction.execute] result:"+result);
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        return null;
    }



    private static Map<String ,Object> getRequestMap(MainTableInfo mainTableInfo){
        Map<String, Object> resultMap = new HashMap<>();
        try {
            // Create a map representing the JSON object
            Map<String, Object> ufinterfaceMap = new HashMap<>();
            ufinterfaceMap.put("account", "01");
            ufinterfaceMap.put("billtype", "project");
            ufinterfaceMap.put("isexchange", "Y");
            ufinterfaceMap.put("replace", "N");
            ufinterfaceMap.put("sender", "hboa");
            ufinterfaceMap.put("groupcode", "HBJSL");

            Map<String, Object> billMap = new HashMap<>();
            billMap.put("billhead", createBillHeadMap(mainTableInfo));

            ufinterfaceMap.put("bill", billMap);
            resultMap.put("ufinterface",ufinterfaceMap);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }

    private static Map<String, Object> createBillHeadMap(MainTableInfo mainTableInfo) {
        Map<String, Object> billHeadMap = new HashMap<>();
        for (Property property : mainTableInfo.getProperty()) {
            // 项目编号
            if (property.getName().equals("xmbh")) {
                billHeadMap.put("project_code", property.getValue());
            }
            // 项目名称
            if (property.getName().equals("xmmc")) {
                billHeadMap.put("project_name", property.getValue());
                billHeadMap.put("project_sh_name", property.getValue());
            }
            // 计划开始日期
            if (property.getName().equals("jhksrq")) {
                billHeadMap.put("plan_start_date", property.getValue());
            }
            // 计划完成日期
            if (property.getName().equals("jhwcrq")) {
                billHeadMap.put("plan_finish_date", property.getValue());
            }
            // 公司编码
            if (property.getName().equals("gsbm")) {
                billHeadMap.put("pk_org", property.getValue());
            }
            // 责任部门
            if (property.getName().equals("bmbm")) {
                billHeadMap.put("pk_duty_dept_v", property.getValue());
            }
            // 责任人
            if (property.getName().equals("zrrbm")) {
                billHeadMap.put("pk_dutier", property.getValue());
            }
            // 项目简要描述
            if (property.getName().equals("xmjyms")) {
                billHeadMap.put("def1", property.getValue());
            }
            // 实施位置
            if (property.getName().equals("xmsswz")) {
                billHeadMap.put("def2", property.getValue());
            }
            // 设备名称
            if (property.getName().equals("sbmc")) {
                billHeadMap.put("def3", property.getValue());
            }
            // 项目分类编码
            if (property.getName().equals("xmflbm")) {
                billHeadMap.put("pk_eps", property.getValue());
            }
            // 预计受益时长
            if (property.getName().equals("xmyjsysc")) {
                billHeadMap.put("def4", property.getValue());
            }
            // 原项目号
            if (property.getName().equals("yxmllk")) {
                billHeadMap.put("def5", property.getValue());
            }
            // 备注
            if (property.getName().equals("bz")) {
                billHeadMap.put("memo", property.getValue());
            }
        }
        // 实际开始日期
        billHeadMap.put("actu_start_date", "");
        // 实际结束日期
        billHeadMap.put("actu_finish_date", "");
        // 创建人
        billHeadMap.put("creator", "hboa");

        return billHeadMap;
    }


}
